package com.huaxia.day15;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolTest {

    public static ThreadPoolExecutor threadPoolExecutor =
            new ThreadPoolExecutor(5,//核心线程数量
                    10,//最大线程数量
                    1000L * 60,//线程空闲时间
                    TimeUnit.MILLISECONDS,//空闲时间单位
                    new ArrayBlockingQueue<>(100),//有界队列 调整该值有惊喜！！！
                    new ThreadPoolExecutor.AbortPolicy());//直接抛出异常，阻止正常工作

    public static void main(String[] args) {
        createTask();

    }


    public static void createTask() {
        for (int i = 0; i < 1000; i++) {
            SendMsgThread send = new SendMsgThread(13114477000L + i + "", "收到我的信息请快速回复");
            threadPoolExecutor.execute(send);
        }
    }

    static class SendMsgThread implements Runnable {
        private String phone;
        private String content;

        SendMsgThread(String phone, String content) {
            this.phone = phone;
            this.content = content;
        }

        @Override
        public void run() {
            try {
                // Thread.sleep(10);
            } catch (Exception e) {

            }
            System.out.print("活动的线程数:" + threadPoolExecutor.getActiveCount());
            System.out.print("，threadName:" + Thread.currentThread().getName() + ",");
            System.out.println("给" + phone + "发送消息：" + content);
        }
    }
}
